// pages/real/myLive/index.js
const app = getApp()
const utils = require('../../../utils/util.js')
import Dialog from '../../../vant-weapp/dist/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    contentTop:'',
    show:false,
    navHeight: app.globalData.navHeight,
    currentStatus:1,
    page:1,
    rows:20,
    contentPage: 1,
    contentRows: 20,
    isRefresh:false,
    hasNoMore:false,
    rightList:[],
    leftList:[],
    tabList:[
      {
        id:1,
        flag:true,
        name:"已通过"
      },
      {
        id:0,
        flag:false,
        name:"审核中"
      },
      {
        id:2,
        flag:false,
        name:"未通过"
      }
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var query = wx.createSelectorQuery()
    query.select('.live-content').boundingClientRect((rect)=>{
      console.log(rect)
      this.setData({
       contentTop:rect.height+app.globalData.navHeight
      })
    }).exec();
    this.handleGetMyLiveContributeList()
  },  
  loadNewData: function() {
    this.setData({
      isRefresh :true
    })
    this.data.page = 1
    this.handleGetMyLiveContributeList()
  },
  loadMoreData:function() {
    console.log(123)
    this.setData({
      page: this.data.page + 1
    })
    this.handleGetMyLiveContributeList(true)
  },
  handleGetMyLiveContributeList(flag=false){
    const userInfo = wx.getStorageSync("userInfo")
    const unionid=wx.getStorageSync("unionid")
    const { currentStatus,page,rows} =this.data
    const str={
      approveState:currentStatus,
      createBy:unionid,
      page,
      rows
    }
    utils.$http(app.globalData.api.getRealVeidoDataList, str, 'POST', true, 'JSON').then(res => {
      const {leftList,rightList} =this.data
      if(res.success){
        const data=res.data.records.map(rr=>{
          rr.deleteFlag=false
          return rr
        }) || []
        if(flag){
          if(data.length===0){
            this.setData({
              hasNoMore:true
            })
          }else{
              if(data.length===1){
               this.setData({
                isRefresh: false,
                 leftList:leftList.concat(data)
               })
              }else{
                data.forEach((item,index)=>{
                  if((index+1)%2===0){
                    rightList.push(item)
                  }else{
                    leftList.push(item)
                  }
                })
                this.setData({
                  isRefresh: false,
                  rightList,
                  leftList
                })
              }
          }
        
        }else{
          const lList=[],rList=[];
          data.forEach((item,index)=>{
            if((index+1)%2===0){
              rList.push(item)
            }else{
              lList.push(item)
            }
          })
          this.setData({
            isRefresh:false,
            hasNoMore: false,
            leftList: lList,
            rightList:rList
          })
        }
      
        // res.data.records.for
      }
    }).catch(()=> {
      this.setData({
        isRefresh: false
      })
    })
  },
 handleOnClose(){
   this.setData({
     show:false
   })
 },
 handleShowReason(){
  this.setData({
    show:true
  })
 },
 handleDeletePro(e){
   const id=e.detail
  Dialog.confirm({
    context: this,
    title: '删除',
    message: '删除后将无法恢复，是否确认删除!',
    className:"submit-class-name"
  })
    .then(() => {
      utils.$http(app.globalData.api.getRealListItemDelete+id, {}, 'Delete', true, 'JSON').then(res => {
        if(res.success){
          wx.showToast({ icon: 'success', title: '删除成功', duration: 1500 })
          this.loadNewData()
        }
      })
    })
    .catch(() => {
      return
    });
 },
  //刷新父组件列表
  handleUpdateProList(e){
    // console.log(e)
    this.handleOnClose()
    const { tabList=[] }=this.data
    const { active,index } = e.currentTarget.dataset
    this.setData({
      currentStatus:active,
      page:1,
      rows:20,
      rightList:[],
      leftList:[]
    })
    tabList.forEach(t=>{
      if(t.id===active){
        t.flag=true
      }else{
        t.flag=false
      }
    })
    this.setData({
      tabList
    })
    this.handleGetMyLiveContributeList()
    // this.data.pageNum = 1
    // this.handleStyleIdProList(this.data.pageNum)
    // this.scrollLeftCompute(index)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})